package com.junior.fixclub.party;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.junior.fixclub.ParamMaker;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class FixPhone {
    public static String TAG = "FixClub";
    public void Fix(XC_LoadPackage.LoadPackageParam loadPackageParam){
        Wifi(loadPackageParam);
        Bluetooth(loadPackageParam);
        Telephony(loadPackageParam);
        //UserAgent(loadPackageParam);
    }

    public void Bluetooth(XC_LoadPackage.LoadPackageParam loadPkgParam) {
        try {
            // 双层 MAC
            XposedHelpers.findAndHookMethod("android.bluetooth.BluetoothAdapter", loadPkgParam.classLoader, "getAddress", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(ParamMaker.BLUETOOTHMAC);
                }

            });
            // 双层MAC
            XposedHelpers.findAndHookMethod("android.bluetooth.BluetoothDevice", loadPkgParam.classLoader, "getAddress", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(ParamMaker.BLUETOOTHMAC);
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"phone MAC HOOK 失败 " + e.getMessage());
        }
    }
    // WIF MAC
    public void Wifi(XC_LoadPackage.LoadPackageParam loadPkgParam) {
        try {
            XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", loadPkgParam.classLoader, "getMacAddress", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(ParamMaker.WIFIMAC);
                }
            });

            // 内网IP
            XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", loadPkgParam.classLoader, "getIpAddress", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(ParamMaker.WIFIIP);
                }
            });
            XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", loadPkgParam.classLoader, "getSSID", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(ParamMaker.WIFINAME);
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"phone MAC HOOK 失败 " + e.getMessage());
        }
        // 基站的信号强度
        XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", loadPkgParam.classLoader, "getBSSID", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                // TODO Auto-generated method stub
                super.afterHookedMethod(param);
                param.setResult(ParamMaker.WIFIBSSID);
            }
        });


    }

    public void UserAgent(XC_LoadPackage.LoadPackageParam loadPackageParam){
        XposedHelpers.findAndHookMethod("android.webkit.WebSettings", loadPackageParam.classLoader, "getUserAgentString", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult(ParamMaker.USERAGENT);
            }
        });
        XposedHelpers.findAndHookMethod("android.webkit.WebSettings", loadPackageParam.classLoader, "getUserAgentString", Context.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult(ParamMaker.USERAGENT);
            }
        });
    }
    /*
    public void getType(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        XposedHelpers.findAndHookMethod("android.net.NetworkInfo", loadPackageParam.classLoader, "getType", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult("getType");
            }
        });
    }
    */
    public void Telephony(XC_LoadPackage.LoadPackageParam loadPkgParam) {
        String TelePhone = "android.telephony.TelephonyManager";
        android.telephony.TelephonyManager telephonyManager;
        try {
            if(Build.VERSION.SDK_INT < 22){
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.gsm.GSMPhone", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.PhoneProxy", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
            }else if(Build.VERSION.SDK_INT == 22){
                XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.PhoneSubInfo", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
            }else if(Build.VERSION.SDK_INT == 23){
                XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant("IMEI"));
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.PhoneSubInfo", loadPkgParam.classLoader, "getDeviceId", String.class, XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
            }else if(Build.VERSION.SDK_INT >= 24){
                XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getDeviceId", XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.PhoneSubInfoController", loadPkgParam.classLoader, "getDeviceIdForPhone", int.class, String.class, XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
                XposedHelpers.findAndHookMethod("com.android.internal.telephony.PhoneSubInfoController", loadPkgParam.classLoader, "getImeiForSubscriber", int.class, String.class, XC_MethodReplacement.returnConstant(ParamMaker.IMEI1));
            }
        } catch (Exception ex) {
            Log.e(TAG," IMEI 错误: " + ex.getMessage());
        }
        /*
        HookTelephony(TelePhone, loadPkgParam, "getDeviceSoftwareVersion", "deviceversion");// 返系统版本
        HookTelephony(TelePhone, loadPkgParam, "getSubscriberId", "IMSI");
        HookTelephony(TelePhone, loadPkgParam, "getLine1Number", "PhoneNumber");
        HookTelephony(TelePhone, loadPkgParam, "getSimSerialNumber", "SimSerial");
        HookTelephony(TelePhone, loadPkgParam, "getNetworkOperator", "networktor"); // 网络运营商类型
        HookTelephony(TelePhone, loadPkgParam, "getNetworkOperatorName", "Carrier"); // 网络类型名
        HookTelephony(TelePhone, loadPkgParam, "getSimOperator", "CarrierCode"); // 运营商
        HookTelephony(TelePhone, loadPkgParam, "getSimOperatorName", "simopename"); // 运营商名字
        HookTelephony(TelePhone, loadPkgParam, "getNetworkCountryIso", "gjISO"); // 国家iso代码
        HookTelephony(TelePhone, loadPkgParam, "getSimCountryIso", "CountryCode"); // 手机卡国家

        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getNetworkType", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                //      网络类型
                param.setResult("networkType");
            }
        });


        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getPhoneType", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult("phonetype");
            }
        });

        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPkgParam.classLoader, "getSimState", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                param.setResult("SimState");
            }
        });
         */
    }

    private void HookTelephony(String hookClass, XC_LoadPackage.LoadPackageParam loadPkgParam, String funcName, final String value) {
        try {
            XposedHelpers.findAndHookMethod(hookClass, loadPkgParam.classLoader, funcName, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult(value);
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"HookTelephony 失败 " + e.getMessage());
        }
    }

}
